package com.zhangdi.dwperms.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Created by zhangdi on 2020/01/14 16:38:14.
 */
public class RestRolesAuthorizationFilter extends RolesAuthorizationFilter {
    @Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        String[] rolesArray = (String[]) mappedValue;
        if (null == rolesArray || rolesArray.length == 0) {
            return true;
        }
        Subject subject = getSubject(request, response);

        for (String perms : rolesArray) {
            if (subject.hasRole(perms)) {
                return true;
            }
        }

        return false;
    }
}
